package com.dean.utils;

import com.dean.entity.TableField;
import com.dean.exception.DeanRuntimeException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName EntityUtils
 * @Description 实体类工具类
 * @Author dean
 * @Date 2021/10/26 12:22
 */
public class DeanEntityUtils {
    private static Map<String,String> covertMap = new HashMap<String,String>();
    private static Map<String,String> classCovertMap = new HashMap<>();
    private static final String BIGDECIMAL = "BigDecimal";
    private static final String DATE = "Date";
    private static final String STRING="String";
    private static final String INTEGER="Integer";
    private static final String LONG="Long";
    private static final String SHORT="Short";


    static {
        covertMap.put("char",STRING);
        covertMap.put("varchar",STRING);
        covertMap.put("varchar2",STRING);
        covertMap.put("clob",STRING);
        covertMap.put("text",STRING);
        covertMap.put("longblob",STRING);
        covertMap.put("int",INTEGER);
        covertMap.put("tinyint",SHORT);
        covertMap.put("smallint",SHORT);

        covertMap.put("bigint",LONG);
        covertMap.put("datetime",DATE);
        covertMap.put("date",DATE);
        covertMap.put("timestamp",DATE);
        covertMap.put("number",BIGDECIMAL);
        covertMap.put("decimal",BIGDECIMAL);
        covertMap.put("numeric",BIGDECIMAL);

        classCovertMap.put(DATE,"java.util.Date");
        classCovertMap.put(BIGDECIMAL,"java.math.BigDecimal");
    }

    /**
     * 根据数据库类型转换为Javabean中的字段类型
     * @param dataType
     * @param dataLen
     * @param dataScale
     * @return
     */
    public static String coverDataType(String dataType,Long dataLen,Integer dataScale){
        dataType = dataType.toLowerCase();
        String entityType = covertMap.getOrDefault(dataType,"");
        if(StringUtils.isBlank(entityType)){
            DeanRuntimeException.throwException(996, String.format("数据类型转换实体类型发生错误：原始数据类型[%s]", dataType));
        }
        // 实体类型为bigdecimal 特殊处理
        if(BIGDECIMAL.equals(entityType)  && (dataScale==null || dataScale == 0) && dataLen!=null && dataLen>0 ){
            if(dataLen < 5 ){
                entityType = SHORT;
            }else if(dataLen < 10){
                entityType = INTEGER;
            }else if(dataLen < 19){
                entityType = LONG;
            }
        }
        return entityType;
    }

    public static List<String> parseImportClasses(List<TableField> tableFields){
        List<String> importClasses = tableFields.stream().
                // 重新组合数据
                map(tableField -> {
                    return classCovertMap.getOrDefault(tableField.getFieldType(),"");
                }).
                // 对组合后的数据进行筛选
                filter(fieldType->StringUtils.isNotBlank(fieldType)).
                // 去重之后组成信息的集合
                distinct().collect(Collectors.toList());
        return importClasses;
    }
}
